home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’87 / Source ƒ.sit / Source ƒ / C ƒ / CITADEL BBS 'C' SRC / LIBTABL.C < prev    next >
C/C++ Source or Header  |  1987-01-14  |  4KB  |  143 lines

  1. /************************************************************************/
  2. /*                libtabl.c                */
  3. /*             Code to handle CTDLTABL.SYS            */
  4. /************************************************************************/
  5.  
  6. /************************************************************************/
  7. /*                history                 */
  8. /*                                    */
  9. /* 86Apr24 HAW    Modified for fwrite() and fread().            */
  10. /* 85Nov15 HAW    Created.                        */
  11. /************************************************************************/
  12.  
  13. #include "ctdl.h"
  14.  
  15. /************************************************************************/
  16. /*                Contents                */
  17. /*                                    */
  18. /*    readSysTab()        restores system state from ctdltabl.sys */
  19. /*    writeSysTab()        saves state of system in CTDLTABL.SYS    */
  20. /************************************************************************/
  21.  
  22. struct config     cfg;            /* A buncha variables        */
  23. struct lTable     *logTab;        /* RAM index of pippuls     */
  24. struct netTable  *netTab;        /* RAM index of nodes        */
  25. struct rTable     roomTab[MAXROOMS];    /* RAM index of rooms        */
  26. char         *indexTable = "ctdlTabl.sys";
  27. static char *msg1 = "?old ctdlTabl.sys!";
  28.  
  29. FILE *safeopen();
  30. char *malloc();
  31.  
  32. /************************************************************************/
  33. /*    readSysTab() restores state of system from CTDLTABL.SYS     */
  34. /*    returns:    TRUE on success, else FALSE            */
  35. /*      destroys CTDLTABL.TAB after read, to prevent erroneous re-use */
  36. /*      in the event of a crash.                    */
  37. /*                                    */
  38. /*    MS-DOS fun: Here's the map --                    */
  39. /*    Word 1 == sizeof cfg                        */
  40. /*    Word 2 == sizeof logTab                     */
  41. /*    Word 3 == sizeof roomTab                    */
  42. /*    Word 4 -- thru x == cfg contents                */
  43. /*    x -- y == logTab                        */
  44. /*    y -- z == roomTab                        */
  45. /*    z -- a == netTab                        */
  46. /*    EOF                                */
  47. /************************************************************************/
  48. readSysTab(kill)
  49. char kill;
  50. {
  51.     unsigned int  getw();
  52.     char      eofflag = FALSE;
  53.     FILE      *fd;
  54.     unsigned int  size1, size2, size3;
  55.  
  56.     if ((fd = safeopen(indexTable, "rb")) == NULL) {
  57.     printf("?no %s!", indexTable);      /* Tsk, tsk! */
  58.     return(FALSE);
  59.     }
  60.  
  61.     size1 = getw(fd);
  62.     eofflag = ferror(fd);
  63.     size2 = getw(fd);
  64.     eofflag = (eofflag) ? eofflag : ferror(fd);
  65.     size3 = getw(fd);
  66.     eofflag = (eofflag) ? eofflag : ferror(fd);
  67.     if (eofflag) {
  68.     printf(msg1);
  69.     return(FALSE);
  70.     }
  71.  
  72.     logTab = (struct lTable *) malloc(size2);
  73.  
  74.                  /* "- 1" is kludge */
  75.     if (!common_read(&cfg, (sizeof cfg), 1, fd))
  76.     return FALSE;
  77.  
  78.     if (size1 != sizeof cfg    ||
  79.     size3 != sizeof roomTab ||
  80.     size2 != sizeof(*logTab) * cfg.MAXLOGTAB) {
  81.     printf(msg1);
  82.     return(FALSE);
  83.     }
  84.  
  85.     if (!common_read(logTab, size2, 1, fd))
  86.     return FALSE;
  87.     if (!common_read(roomTab, sizeof roomTab, 1, fd))
  88.     return FALSE;
  89.     netTab = (struct netTable *) malloc(sizeof (*netTab) * cfg.netSize);
  90.     if (cfg.netSize)
  91.     if (!common_read(netTab, (sizeof(*netTab) * cfg.netSize), 1, fd))
  92.         return FALSE;
  93.  
  94.     if (kill) unlink(indexTable);
  95.     return(TRUE);
  96. }
  97.  
  98. /************************************************************************/
  99. /*    common_read() reads in from file the important stuff        */
  100. /*    returns:    TRUE on success, else FALSE            */
  101. /************************************************************************/
  102. static common_read(block, size, elements, fd)
  103. char *block;
  104. unsigned size;
  105. unsigned elements;
  106. FILE *fd;
  107. {
  108.     if (fread(block, size, elements, fd) != 1) {
  109.     printf(msg1);
  110.     return FALSE;
  111.     }
  112.     return TRUE;
  113. }
  114.  
  115. /************************************************************************/
  116. /*    writeSysTab() saves state of system in CTDLTABL.SYS        */
  117. /*    returns:    TRUE on success, else ERROR            */
  118. /*    See readSysTab() to see what the CTDLTABL.SYS map looks like.    */
  119. /************************************************************************/
  120. writeSysTab()
  121. {
  122.     unsigned char *c;
  123.     FILE      *fd;
  124.  
  125.     if ((fd = safeopen(indexTable, "wb")) == NULL) {
  126.     printf("?can't make %s", indexTable);
  127.     return(ERROR);
  128.     }
  129.  
  130.     /* Write out some key stuff so we can detect bizarreness: */
  131.     putw(sizeof cfg    , fd);
  132.     putw(sizeof(*logTab) * cfg.MAXLOGTAB, fd);
  133.     putw(sizeof roomTab, fd);
  134.  
  135.     fwrite(&cfg, (sizeof cfg), 1, fd);
  136.     fwrite(logTab, (sizeof(*logTab) * cfg.MAXLOGTAB), 1, fd);
  137.     fwrite(roomTab, sizeof roomTab, 1, fd);
  138.     fwrite(netTab, (sizeof(*netTab) * cfg.netSize), 1, fd);
  139.  
  140.     fclose(fd);
  141.     return(TRUE);
  142. }
  143.